package cn.qqncn.furniture.controller;

import cn.qqncn.furniture.mapper.UserMapper;
import cn.qqncn.furniture.pojo.domain.User;
import cn.qqncn.furniture.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author zkw
 * @Description 用户控制层
 * @date 2020年11月30日 13点05分
 */
@CrossOrigin
@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    UserMapper userMapper;

    @Autowired
    UserService userService;

    final ObjectMapper objectMapper = new ObjectMapper();


    /**
     * 检查用户名是否存在
     * @param username
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/checkUsername",method = RequestMethod.GET)
    @ResponseBody
    public String checkUsername(String username) throws JsonProcessingException {
        boolean flag = userService.checkUsername(username);
        ObjectNode node = objectMapper.createObjectNode();
        node.put("boolean",flag);
        return objectMapper.writeValueAsString(node);
    }

    /**
     * 用户注册
     * @param user
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ModelAndView register(User user) throws JsonProcessingException {
        userService.register(user);
        ModelAndView modelAndView = new ModelAndView("/front/login-register");
        modelAndView.addObject("flag",true);
        return modelAndView;
    }

    /**
     * 登录
     * @param modelAndView
     * @param user
     * @return ModelAndView
     */
    @PostMapping("/login")
    public ModelAndView login(HttpServletRequest request, ModelAndView modelAndView, User user){
        User login = userService.login(user);
        if(login!=null){
            request.getSession().setAttribute("user",login);    //session
            modelAndView.addObject("user",login);
            modelAndView.addObject("flag",true);
            modelAndView.setViewName("/front/index");
        }else {
            modelAndView.addObject("flag",false);
            modelAndView.setViewName("/front/login-register");
        }
        return modelAndView;
    }


    /**
     * 退出登录
     * @param request
     * @return 返回首页
     */
    @RequestMapping("loginOut")
    public String loginOut(HttpServletRequest request){
        request.getSession().setAttribute("user",null);
        return "front/index";
    }


    /**
     * 查询用户列表
     * @return
     */
    @GetMapping("get")
    @ResponseBody
    public String resultSet() throws JsonProcessingException {
        List<User> list = userService.searchAll();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",list.size());
        map.put("data",list);
        return objectMapper.writeValueAsString(map);
    }

}
